package com.uml.youmath;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/** NewGame class Activity. This is the class that is where the actual
* gameplay exists.
* TODO: Should this be renamed GameScreen or something similar?
*/
public class NewGame extends Activity implements OnClickListener {
	Button btn_next2;
	TextView questionText, timerText;
	RadioButton option1, option2, option3, option4;
	RadioGroup radioGroup1;
	MyCountDownTimer countDownTimer;
	ProblemMaker pm = null;
	HashMap<Integer, String> question;
	MediaPlayer soundeffect = null;
	Toast toast;
	int correct = 0;
	int currentPos = 0;
	
	// Set Count Down time and intervals between each tick call
	final long startTime = 60000;
	final long interval = 100;

	int end_id = 0, answers = 0, gameType = 0, stageSelect = 0;
	
	ArrayList<Integer> randAnswer = new ArrayList<Integer>();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.newgame);
			btn_next2 = (Button) findViewById(R.id.btn_next2);
			btn_next2.setOnClickListener(this);
			btn_next2.setVisibility(View.INVISIBLE);
			
			// Shot Clock view
			timerText = (TextView) this.findViewById(R.id.timer);
			
			//initialize sounds
			SoundService.initSounds(this);
			
			// Create Count Down Timer object and begin count down
			countDownTimer = new MyCountDownTimer(startTime, interval);
			timerText.setText("Shot Clock: 1:00");
			countDownTimer.start();
			
			// get data from previous activity
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				gameType = extras.getInt("gametype");
				stageSelect = extras.getInt("stageselect");
			}
			
			pm = new ProblemMaker();
			
			question = pm.GetProblem(gameType, stageSelect);

			questionText = (TextView) findViewById(R.id.questionText);
			questionText.setText(question.get(ProblemMaker.QUESTION).toString());

			radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);

			option1 = (RadioButton) findViewById(R.id.option1);
			option2 = (RadioButton) findViewById(R.id.option2);
			option3 = (RadioButton) findViewById(R.id.option3);
			option4 = (RadioButton) findViewById(R.id.option4);
			
			final ImageView playerOne = (ImageView) findViewById(R.id.p_1);
		    playerOne.setBackgroundResource(R.drawable.p_jump_animation);
		    playerOne.post(new Runnable() {
		    	@Override
		    	public void run() {
		    		AnimationDrawable pJumpAnim = (AnimationDrawable) playerOne.getBackground();
		    		pJumpAnim.start();
		    	}
		    });
		    
		    final ImageView playerTwo = (ImageView) findViewById(R.id.p_2);
		    playerTwo.setBackgroundResource(R.drawable.p2_jump_animation);
		    playerTwo.post(new Runnable() {
		    	@Override
		    	public void run() {
		    		AnimationDrawable p2JumpAnim = (AnimationDrawable) playerTwo.getBackground();
		    		p2JumpAnim.start();
		    	}
		    });
		    
		    final ImageView playerThree = (ImageView) findViewById(R.id.p_3);
		    playerThree.setBackgroundResource(R.drawable.p3_jump_animation);
		    playerThree.post(new Runnable() {
		    	@Override
		    	public void run() {
		    		AnimationDrawable p3JumpAnim = (AnimationDrawable) playerThree.getBackground();
		    		p3JumpAnim.start();
		    	}
		    });
			
			if ( gameType == ProblemMaker.NUMBER_IDENTITY ) {
				randAnswer.add(1);
				randAnswer.add(2);
				
				option2.setText("True");
				option2.setOnClickListener(this);

				option3.setText("False");
				option3.setOnClickListener(this);

				option1.setVisibility(View.INVISIBLE);
				option4.setVisibility(View.INVISIBLE);
			} else {
				randAnswer.add(1);
				randAnswer.add(2);
				randAnswer.add(3);
				randAnswer.add(4);
			
				Collections.shuffle(randAnswer);

				option1.setText(question.get(randAnswer.get(0)));
				option1.setOnClickListener(this);

				option2.setText(question.get(randAnswer.get(1)));
				option2.setOnClickListener(this);

				option3.setText(question.get(randAnswer.get(2)));
				option3.setOnClickListener(this);

				option4.setText(question.get(randAnswer.get(3)));
				option4.setOnClickListener(this);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		try {
			int answer = 0 ;
			
			if ( gameType != ProblemMaker.NUMBER_IDENTITY) {
				answer = Integer.parseInt(question.get(1));
			}
			if ( gameType == ProblemMaker.NUMBER_IDENTITY && ( v == option2 || v == option3 ) ) {
				if ( question.get(1) == ((RadioButton)v).getText()) {
					toast = Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 50);
					toast.show();
					
					playCrowdCheer();
					
					answers++;
					correct++;
					
					reSetup();

				} else {
					toast = Toast.makeText(getApplicationContext(), "Incorrect!", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 50);
					toast.show();
					
					playCrowdBoo();
					
					answers++;
					
					reSetup();
				}
			} else if (v != btn_next2
					&& answer == Integer.parseInt(((RadioButton)v).getText().toString())) {
				toast = Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 50);
				toast.show();
				
				playCrowdCheer();
			
				answers++;
				correct++;
				
				reSetup();
				
			} else if (v == btn_next2) {

				finish();
				return;
				
			} else {
				
				toast = Toast.makeText(getApplicationContext(), "Incorrect!", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 50);
				toast.show();
				
				playCrowdBoo();
				
				answers++;
				
				reSetup();				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class MyCountDownTimer extends CountDownTimer
	{
		public MyCountDownTimer(long startTime, long interval)
		{
			super(startTime, interval);
		}
		
		@Override
		public void onFinish()
		{
			
			Intent i = new Intent(NewGame.this, ScoreScreen.class);
			i.putExtra("Score", correct);
			i.putExtra("stageSelect", stageSelect);
			startActivity(i);
			/*timerText.setText("Shot Clock: 00.0");
			end_id = 1;
			
			option1.setVisibility(View.INVISIBLE);
			option2.setVisibility(View.INVISIBLE);
			option3.setVisibility(View.INVISIBLE);
			option4.setVisibility(View.INVISIBLE);
			btn_next2.setVisibility(View.VISIBLE);*/
			
		}
		
		@Override
		public void onTick(long msUntilFinished)
		{
			int seconds = (int) (msUntilFinished/1000);

			if ( (seconds % 60) < 10 ) {
				timerText.setText("Shot Clock: 0" + (seconds % 60)
						+ "." + (msUntilFinished % 1000)/100);
			} else {
				timerText.setText("Shot Clock: " + (seconds % 60)
						+ "." + (msUntilFinished % 1000)/100);
			}
		}
	}
	
	public void reSetup(){
		question = pm.GetProblem(gameType, stageSelect);
		questionText.setText(question.get(ProblemMaker.QUESTION).toString());
		
		if ( gameType != ProblemMaker.NUMBER_IDENTITY) {
			
			Collections.shuffle(randAnswer);
		
			option1.setText(question.get(randAnswer.get(0)));
			option2.setText(question.get(randAnswer.get(1)));
			option3.setText(question.get(randAnswer.get(2)));
			option4.setText(question.get(randAnswer.get(3)));
		}
		radioGroup1.clearCheck();
	}
	
	public void playCrowdCheer(){
		SoundService.playSound(this, SoundService.CHEERS);
	}
	
	public void playCrowdBoo(){
		SoundService.playSound(this, SoundService.BOO);
	}
	
	@Override
	public void onPause() {
		currentPos = SoundService.musicPlayer.getCurrentPosition();
		SoundService.currentPos = currentPos;
		SoundService.musicPlayer.pause();
		super.onPause();
	}
	
	@Override
	public void onResume() {
		SoundService.resumeAt();
		super.onResume();
	}
}